package com.liuya.common;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;

public class IOUtil {
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }

    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }

    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    public static void write(String dir, String file, String content) throws IOException {
        write(dir + file, content);
    }

    public static void write(String file, String content) throws IOException {
        OutputStreamWriter bos = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        bos.write(content);
        bos.close();
    }

    public static String read(String dir, String file) throws IOException {
        return read(dir + file);
    }

    public static String read(String file) throws IOException {
        StringBuffer content = new StringBuffer();
        char[] buff = new char[1024];
        InputStreamReader bis = new InputStreamReader(new FileInputStream(file), "UTF-8");

        int read = bis.read(buff);
        while (read != -1) {
            content.append(new String(buff, 0, read));
            read = bis.read(buff);
        }

        bis.close();

        return content.toString();
    }

    /**
     * 返回项目的真实部署路径
     * @return
     */
    public static String getRealPath(String path) {
        String result = SpringUtils.getHttpServletRequest().getServletContext().getRealPath(path);
        result = result.replace("\\","/").substring(0, result.length()-1);
        return result;
    }

    /**
     * 返回项目的真实部署路径
     * @return
     */
    public static String getRootRealPath() {
        String path = "";
//        //返回项目的应用目录，如web项目返回D:\servers\idea-tomcat-8.0.26\bin
//        System.out.println(System.getProperty("user.dir"));
//        //返回项目的应用目录，如web项目返回D:\servers\idea-tomcat-8.0.26\webapps\ROOT\
//        System.out.println(SpringUtils.getHttpServletRequest().getRealPath("/"));
//        System.out.println(SpringUtils.getHttpServletRequest().getServletContext().getRealPath("/"));
//        System.out.println(SpringUtils.getHttpServletRequest().getServletContext().getRealPath(""));
//        System.out.println(SpringUtils.getHttpServletRequest().getServletContext().getRealPath("/"));
//        System.out.println(SpringUtils.getHttpServletRequest().getSession().getServletContext().getRealPath(""));
//        //使用classpath方式得到绝对路径，返回/D:/servers/idea-tomcat-8.0.26/webapps/ROOT/WEB-INF/classes/
//        System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
        path = SpringUtils.getHttpServletRequest().getServletContext().getRealPath("");
        path = path.replace("\\","/").substring(0, path.length()-1);
        return path;
    }
}